local Status = require("scene.Status")
local Msg = require("core.Msg")
local Obj = require("core.Obj")
local BufferExcel = require("excel.Buffer").Buff
local Buffer = require("buffer.Buffer")
local EffectLogic = require("scene.EffectLogic")
local Cmds = require("buffer.Cmds")
local Utils = require("buffer.Utils")

function sendBufferList(obj, not_send_when_empty,mustSend)
	if obj.obj_type == Obj.TYPE_HUMAN or obj.obj_type == Obj.TYPE_MONSTER or obj.obj_type == Obj.TYPE_PET then  
		local mm = Msg.gc.GC_BUFFER_LIST
		setBufferList(obj,mm.list)
		mm.objID = obj.id
		if obj.hide then
			if obj.obj_type == Obj.TYPE_HUMAN and (not not_send_when_empty or 0 < mm.list[0]) then
				Msg.send(mm,obj.fd)
			end
		elseif mustSend or obj.bufChange ~=obj.bufChangeSend then	
--			Msg.send(mm,obj.fd)
        Obj.sendArea(mm, obj.scene_id, obj.x, obj.y)
		end	
		
		if obj.obj_type == Obj.TYPE_HUMAN then
		    Status.sendControlStatus(obj)
		end
	end 
	
--	if obj.bufChange ~=obj.bufChangeSend then
--		obj.bufChangeSend = obj.bufChange
		sendBuffertEffect(obj)
		sendBufferType(obj)
--	end
end

function sendBuffertEffect(obj)

	local mm = Msg.gc.GC_BUFFER_EFFECT
	mm.objID = obj.id
	
	setBufferEffect(obj,mm)
	
	if obj.obj_type == Obj.TYPE_HUMAN and obj.hide then
		Msg.send(mm, obj.fd)
	else
		Obj.sendArea(mm, obj.scene_id, obj.x, obj.y)
	end
	
end

local effectPosTemp = {}
function setBufferEffect(obj,t)	

	for k in pairs(effectPosTemp) do
		effectPosTemp[k] = nil
	end
	
	local color = Buffer.getBufferColor(obj)
	local scale = Buffer.getBufferScale(obj)
	local size = 0
	
	local effects = t.effects
	local bufList, len = Obj.getBuf(obj)
	local getConfig = EffectLogic.getConfig
	for i=len-1,0,-1 do
		local buf = bufList[i]
		local config = BufferExcel[buf.id]
		local effectID = getEffectID(buf)
		local pos = config.pos
		if effectID ~=0 and not effectPosTemp[pos] then
				size = size+1
				local bufferEffect = effects[size]
				bufferEffect.pos = pos
				bufferEffect.effectID = effectID
				bufferEffect.effectConfig = getConfig(effectID)
				effectPosTemp[pos] = effectID
		end
	end
	
	effects[0] = size	
	t.color = color
	t.scale = scale
end

function sendBufferType(obj)
	local typeArgs = obj.bufAttr.typeArgs
	if not typeArgs and not obj.oldSendBufferType then
		return 
	end
	
	obj.oldSendBufferType = typeArgs
		
	local mm = Msg.gc.GC_BUFFER_TYPE
	mm.objID = obj.id
	setBufferType(obj,mm.list)
	
	if obj.obj_type == Obj.TYPE_HUMAN and obj.hide then
		Msg.send(mm, obj.fd)
	else
		Obj.sendArea(mm, obj.scene_id, obj.x, obj.y)
	end
end

function setBufferType(obj,list)
	local CmdsSetTypeArgs = Cmds.setTypeArgs
	local bufList, bufLen = Obj.getBuf(obj)
	local len = 0
	for i=0, bufLen-1 do
		local buf =bufList[i]
		local config = BufferExcel[buf.id]
		local setTypeArgs = CmdsSetTypeArgs[config.cmd]
		if setTypeArgs then
				len = len + 1
				local TypeArgsData = list[len]
				setTypeArgs(obj,buf,TypeArgsData.typeArgs)
				if len>=16 then
					break
        end
		end
	end	
	list[0] = len
end

local max = math.max
function getEffectID(buf)
	local config = BufferExcel[buf.id]
	local effectID = config.effectID
	return (effectID[max(buf.count,1)] or effectID[1]) or 0
end

function setBufferList(obj, tb)
	local bufList, bufLen = Obj.getBuf(obj)
	local len = 0
	for i=0,bufLen-1 do
		local buf =bufList[i]
		local config = BufferExcel[buf.id]
  	len = len + 1
	  local buff = tb[len]	
	  Utils.setBuffInfo(obj,buff, buf, config)	
		if len>=16 then
				break
		end
	end	
	tb[0] = len
end

function setBufferInfo(obj,bufferInfo)
	setBufferEffect(obj,bufferInfo)
	setBufferType(obj,bufferInfo.listType)
end

